Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
mongodb - match if a group of values from an array is in another given array [with Aggregation]

I'm looking for a solution since 3 hours and I don't get it yet.

I have the following collection:

{text: 'random text 1', indexes:[1,2] },
{text: 'random text 2', indexes:[1,3] },
{text: 'random text 3', indexes:[2,4] },

and I would like to have only the documents that have all the index values in a given array like for example [1,2,4]

using the example above, I would like to have the following output:

{text: 'random text 1', indexes:[1,2] },
{text: 'random text 3', indexes:[2,4] },

[1,2] is in [1,2,4] -> OK

[1,3] is not in [1,2,4] because of 3 -> Not OK

[1,4] is in [1,2,4] -> OK

Any idea? thanks for your answer! :)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use one of this this $match stage:

  • Using $setIsSubset into $expr:
{
  "$match": {
    "$expr": {
      "$setIsSubset": ["$indexes",[1,2,4]]
    }
  }
}

Example here

  • Using $elemMatch and double negation ($not and $nin):
{
  "$match": {
    "indexes": {
      "$not": {
        "$elemMatch": {
          "$nin": [1,2,4]
        }
      }
    }
  }
}

Example here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda